package singleton;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * 注册式单例父类
 *
 * @author zhangxi
 * @date 2021/1/12
 */
public class RegionSingle {

    private static HashMap registryMap = new HashMap();

    static {
        RegionSingle regionSingle = new RegionSingle();
        registryMap.put(RegionSingle.class.getName(), regionSingle);
    }

    protected RegionSingle() {
    }

    public static RegionSingle getInstance(String className) {
        if (StringUtils.isBlank(className)) {
            className = RegionSingle.class.getName();
        }
        if (registryMap.get(className) == null) {
            try {
                registryMap.put(className, Class.forName(className).newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return (RegionSingle) registryMap.get(className);
    }

    private String about(){
        return "hello i am RegionSingle";
    }
}
